package com.example.myapplication.module1.activity

import android.os.Bundle
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.DividerItemDecoration
import com.example.myapplication.databinding.ActivityWorkNoticeBinding
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.entity.WorkNoticeBean
import com.example.myapplication.module1.adapter.WorkNoticeAdapter
import com.example.myapplication.module1.presenter.WorkNoticePresenter
import com.example.myapplication.soaprequest.SoapEnvelop
import com.example.myapplication.soaprequest.request.BroadcastpromptRequest
import com.example.myapplication.soaprequest.request.SoapRequestBody
import com.example.myapplication.widget.CustomerToolbar
import com.example.mylibrary.adapter.BaseRecyclerAdapter
import com.example.mylibrary.base.BaseMvpActivity
import com.example.mylibrary.utils.GsonUtil
import com.example.mylibrary.utils.SpUtils
import com.example.mylibrary.utils.UiTools
import com.google.gson.reflect.TypeToken
import com.lxj.xpopup.impl.LoadingPopupView
import java.math.BigDecimal

/**
 * 工作提醒
 */
class WorkNoticeActivity :
    BaseMvpActivity<WorkNoticeActivity, WorkNoticePresenter, ActivityWorkNoticeBinding>() {
    private val soapEnvelop = SoapEnvelop()
    private val soapRequestBody = SoapRequestBody()
    private val bundle = Bundle()
    private val broadcastprompt = BroadcastpromptRequest()
    var loadPopup: LoadingPopupView? = null
    private var itemBean: ItemBean? = null

    private val workNoticeAdapter: WorkNoticeAdapter by lazy {
        WorkNoticeAdapter(this)
    }

    override fun getBinding(): ActivityWorkNoticeBinding {
        return ActivityWorkNoticeBinding.inflate(layoutInflater)
    }

    override fun getPresenter(): WorkNoticePresenter = WorkNoticePresenter()

    override fun initData() {

        viewBinding.list.srlRefresh.setEnableRefresh(false)
        viewBinding.list.srlRefresh.setEnableLoadMore(false)

        itemBean = bundleExtra?.getParcelable<ItemBean>("itemInfo")
        viewBinding.clToolbar.setTitle(itemBean?.itemName)
        loadPopup = builder.asLoading("加载中...")
        workNoticeAdapter.clickListener = object : BaseRecyclerAdapter.ClickListener {
            override fun clickListener(position: Int, view: View?) {
                val workNoticeBeans = workNoticeAdapter.workNoticeBeans
                if (workNoticeBeans.size > 0) {
                    val workNoticeBean = workNoticeBeans[position]
                    if (BigDecimal(workNoticeBean.cont).toDouble() > BigDecimal.ZERO.toDouble()) {
                        bundle.putParcelable("workNotice", workNoticeBean)
                        startNextActivity(
                            this@WorkNoticeActivity,
                            bundle,
                            WorkNoticeDetailActivity::class.java
                        )
                    } else {
                        UiTools.showToast("暂无更多内容")
                    }
                }
            }

            override fun longClickListener(position: Int, view: View?): Boolean {
                return false
            }
        }

        viewBinding.list.rvList.addItemDecoration(
            DividerItemDecoration(
                this,
                LinearLayout.VERTICAL
            )
        )
        viewBinding.list.rvList.adapter = workNoticeAdapter

        workNotice()
    }

    // 获取工作提醒
    private fun workNotice() {
        broadcastprompt.opid = SpUtils.get("opId", "") as String
        broadcastprompt.dbname = SpUtils.get("dbname", "") as String
        soapRequestBody.value = broadcastprompt
        soapEnvelop.body = soapRequestBody
        mPresenter?.getWorkNotice(soapEnvelop)
    }

    override fun initView() {

        viewBinding.clToolbar.clickListener = object : CustomerToolbar.ClickListener {
            override fun onBackListener() {
                finish()
            }

            override fun onRightListener() {

            }
        }
    }


    fun setWorkNotice(result: String) {
        if (result.isNotEmpty()) {
            val workNotices = GsonUtil.fromJson<MutableList<WorkNoticeBean>>(
                result,
                object : TypeToken<MutableList<WorkNoticeBean>>() {}.type
            )
            workNoticeAdapter.workNoticeBeans = workNotices
        }
    }


}
